package regpang.myapplication22.dialog;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.view.ViewGroup.LayoutParams;

import java.util.List;

import regpang.myapplication22.R;

public class PopupWindowDialog extends PopupWindow implements View.OnClickListener {
    private View rootView;//弹窗的父布局，如果
    private List<String> list;

    private OnTextSelectListener textSelectListener;

    public PopupWindowDialog(final Context context, final List<String> list, int currentItem) {
        super(context);
        this.list = list;
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.MATCH_PARENT);
        this.setBackgroundDrawable(new ColorDrawable(0xb0000000));
        this.setOutsideTouchable(true);
        //设置动画效果
        this.setAnimationStyle(R.style.popwindow_anim_style);

        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        rootView = mLayoutInflater.inflate(R.layout.popupwindow_dialog, null);
        FrameLayout tvCancel = rootView.findViewById(R.id.fl_navi);
        tvCancel.setOnClickListener(this);
        setContentView(rootView);

        //这里是用来解决点击黑色背景不消失的问题
        rootView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = rootView.findViewById(R.id.dialog_content).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        textSelectListener.onTextSelect("点击外部消失了");
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.fl_navi) {
            dismiss();
            return;
        }
    }

    public interface OnTextSelectListener {
        //这里是弹窗的点击回调，哪里用自己调用
        void onTextSelect(String deviceTypeName);
    }

    public void setOnTextSelectListener(OnTextSelectListener textSelectListener) {
        this.textSelectListener = textSelectListener;
    }

}
